﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using BrainTechLLC;
using BrainTechLLC.WPF;

namespace BrainTechLLC.WPF
{
    public static class Sorting
    {
#if SILVERLIGHT
#else
        private static Dictionary<ItemsControl, GridViewColumnHeader> LastHeadersClicked = new Dictionary<ItemsControl, GridViewColumnHeader>();
#endif
        private static Dictionary<ItemsControl, ListSortDirection> LastDirection = new Dictionary<ItemsControl, ListSortDirection>();

        private static Dictionary<CollectionViewSource, string> LastSort = new Dictionary<CollectionViewSource, string>();

        public static void Sort(this CollectionViewSource lv, string sortBy, ListSortDirection direction)
        {
            ICollectionView dataView = lv.View;

            dataView.SortDescriptions.Clear();
            SortDescription sd = new SortDescription(sortBy, direction);
            dataView.SortDescriptions.Add(sd);
            dataView.Refresh();
        }

        public static void HandleSorting(this CollectionViewSource source, string thisSort, string path, ListSortDirection direction)
        {
            Sort(source, path, direction);
            LastSort[source] = thisSort;
        }

#if SILVERLIGHT
#else
        public static void HandleColumnHeaderClick(this ItemsControl lst, GridViewColumnHeader headerClicked)
        {
            DataTemplate dt1 = CreateSortDataTemplate("M 5,10 L 15,10 L 10,5 L 5,10");
            DataTemplate dt2 = CreateSortDataTemplate("M 5,5 L 10,10 L 15,5 L 5,5");

            ListSortDirection direction;
            GridViewColumnHeader lastHeaderClicked = null;
            ListSortDirection lastDirection;

            LastHeadersClicked.TryGetValue(lst, out lastHeaderClicked);

            if (!LastDirection.TryGetValue(lst, out lastDirection))
            {
                lastDirection = ListSortDirection.Ascending;
            }

            if (headerClicked != null && headerClicked.Role != GridViewColumnHeaderRole.Padding)
            {
                direction = (headerClicked != lastHeaderClicked) ? ListSortDirection.Ascending :
                            ((lastDirection == ListSortDirection.Ascending) ? ListSortDirection.Descending : ListSortDirection.Ascending);

                string bindingPath;

                Binding displayBinding = headerClicked.Column.DisplayMemberBinding as Binding;

                if (displayBinding != null)
                {
                    bindingPath = (headerClicked.Column.DisplayMemberBinding as Binding).Path.Path as string;
                }
                else
                {
                    bindingPath = headerClicked.Tag as string;
                }

                Sort(lst, bindingPath, direction);

                if (direction == ListSortDirection.Ascending)
                {
                    headerClicked.Column.HeaderTemplate = dt1;
                }
                else
                {
                    headerClicked.Column.HeaderTemplate = dt2;
                }

                // Remove arrow from previously sorted header
                if (lastHeaderClicked != null && lastHeaderClicked != headerClicked)
                {
                    lastHeaderClicked.Column.HeaderTemplate = null;
                }

                LastHeadersClicked[lst] = headerClicked;
                LastDirection[lst] = direction;
            }
        }
#endif

#if SILVERLIGHT
#else
        private static DataTemplate CreateSortDataTemplate(string path)
        {
            DataTemplate dt = new DataTemplate();
            FrameworkElementFactory dpFactory = new FrameworkElementFactory(typeof(DockPanel));
            dt.VisualTree = dpFactory;
            FrameworkElementFactory tbFactory = new FrameworkElementFactory(typeof(TextBlock));
            tbFactory.SetBinding(TextBlock.TextProperty, new Binding());
            dpFactory.AppendChild(tbFactory);
            GeometryConverter gc = new GeometryConverter();
            FrameworkElementFactory pathFactory = new FrameworkElementFactory(typeof(Path));
            pathFactory.SetValue(Path.StrokeThicknessProperty, 1.0D);
            pathFactory.SetValue(Path.FillProperty, Brushes.Gray);
            pathFactory.SetValue(Path.DataProperty, gc.ConvertFrom(path) as Geometry);
            dpFactory.AppendChild(pathFactory);
            return dt;
        }
#endif


#if SILVERLIGHT
#else
        public static void Sort(this ItemsControl lv, string sortBy, ListSortDirection direction)
        {
            ICollectionView dataView = CollectionViewSource.GetDefaultView(lv.ItemsSource);

            dataView.SortDescriptions.Clear();
            SortDescription sd = new SortDescription(sortBy, direction);
            dataView.SortDescriptions.Add(sd);
            dataView.Refresh();
        }
#endif
    }
}
